package com.mercadoutils.controller;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;


import com.csvreader.CsvReader;
import com.mercadoutils.domain.Items;
import com.mercadoutils.listeners.SesionListener;
import com.mercadoutils.singletons.PoolConectionsSingleton;

@Controller
public class upCsvController{
	@RequestMapping("/uploadCsv.html")
    public String handleRequest(Model model,@RequestParam(value="upCsv",required=false,defaultValue="no") String upCsv,
    										@RequestParam(value="fileName",required=false) String fileName){
		if(upCsv.equals("yes")){
			CsvReader item_import;
			HttpSession session = SesionListener.getSession();	    	
			try {
				Connection conexion = PoolConectionsSingleton.getConection();  	
				Statement st = conexion.createStatement();
				item_import = new CsvReader(System.getProperty("catalina.base")+"/wtpwebapps/MercadoUtils/csv/"+fileName);
				item_import.setDelimiter(';');
				item_import.readHeaders();
				
				while (item_import.readRecord())
				{
					String title= item_import.get("Titulo");
					String subtitle= item_import.get("SubTitulo");
					String price= item_import.get("Precio");
					String available_quantity= item_import.get("Cantidad");
					String description= item_import.get("Descripcion");
					String condition= item_import.get("Condicion");
					String imagen= item_import.get("Imagen");
					st.executeUpdate("insert into item (title,subtitle,price,available_quantity,description,item_condition,userId,status,picture) values ('"+title.replaceAll("\'", " ")+"','"+subtitle.replaceAll("\'", " ")+"',"+price.replaceAll("," ,".")+","+available_quantity+",'"+description.replaceAll("\'", " ")+"','"+condition+"',"+session.getAttribute("userId")+",'eter','"+imagen+"')");
					
				}
				
				item_import.close();
			} catch (FileNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (ServletException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			model.addAttribute("nombre",session.getAttribute("nombre"));
			model.addAttribute("apellido",session.getAttribute("apellido"));
			model.addAttribute("role",session.getAttribute("role"));
			model.addAttribute("srcImagen","http://localhost:8080/MercadoUtils/imagenesDB/"+session.getAttribute("srcImagen"));
				return "redirect:/productListPendientes.html";
		}
		else{
			HttpSession session = SesionListener.getSession();
			model.addAttribute("nombre",session.getAttribute("nombre"));
			model.addAttribute("apellido",session.getAttribute("apellido"));
			model.addAttribute("role",session.getAttribute("role"));
			model.addAttribute("srcImagen","http://localhost:8080/MercadoUtils/imagenesDB/"+session.getAttribute("srcImagen"));
			return "uploadCsv";
		}
	} 
	
}